﻿using System;
using System.Collections.Generic;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;


namespace Model {

  public class Endereco {

    private string idEndereco;
    private string logradouro;
    private string complemento;
    private int numero;
    private string cep;
    private string bairro;
    private Uf uf;
    private Cidade cidade;

    public Endereco()
    {
    }

    public Endereco(string pIdEndereco, string pLogradouro, string pComplemento,
        int pNumero, string pCep, string pBairro, Uf pUf, Cidade pCidade)
    {
        this.IdEndereco = pIdEndereco;
        this.Logradouro = pLogradouro;
        this.Complemento = pComplemento;
        this.Numero = pNumero;
        this.Cep = pCep;
        this.Uf = pUf;
        this.Cidade = pCidade;
    }

    public string IdEndereco {
      get { return idEndereco; }
      set { idEndereco = value; }
    }

    public string Logradouro
    {
      get { return logradouro; }
      set { logradouro = value; }
    }


    public string Complemento
    {
      get { return complemento; }
      set { complemento = value; }
    }


    public int Numero {
      get { return numero; }
      set { numero = value; }
    }

    public string Cep
    {
      get { return cep; }
      set { cep = value; }
    }
  
    public string Bairro
    {
        get { return bairro; }
        set { bairro = value; }
    }

    public Uf Uf
    {
        get { return uf; }
        set { uf = value; }
    }

    public Cidade Cidade
    {
        get { return cidade; }
        set { cidade = value; }
    }

    #region Persist
    public Boolean Persist()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Save(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Update
    public Boolean Update()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Update(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Delete
    public static Boolean Delete(int pIdEndereco)
    {
        Endereco objEndereco = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objEndereco = (Endereco)objSession.Load(typeof(Model.Endereco), pIdEndereco);
            objSession.Delete(objEndereco);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region RetriveObject
    public static Endereco RetriveObject(string pIdEndereco)
    {
        Endereco objEndereco = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objEndereco = (Endereco)objSession.Load(typeof(Model.Endereco), pIdEndereco);
            objSession.Close();
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
        return objEndereco;
    }
    #endregion

    #region RetriveObjects
    public static IList RetriveObjects()
    {
        IList listEndereco;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            listEndereco = objSession.CreateCriteria(typeof(Model.Endereco)).List();
            objSession.Close();

        }
        catch (Exception ex)
        {
            listEndereco = null;
            objTransaction.Rollback();
            throw ex;
        }
        return listEndereco;
    }
    #endregion
  }
}
